package com.nc.cloud.admin.biz;

import com.nc.cloud.admin.entity.RoleMenu;
import com.nc.cloud.admin.entity.UserRole;
import com.nc.cloud.common.context.BaseContextHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nc.cloud.admin.entity.Menu;
import com.nc.cloud.admin.mapper.MenuMapper;
import com.nc.cloud.common.biz.BaseBiz;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author XYP
 * @email 23484376@qq.com
 * @date 2020-03-20 09:33:52
 */
@Service
public class MenuBiz extends BaseBiz<MenuMapper, Menu> {

    @Autowired
    MenuMapper menuMapper;

    @Autowired
    RoleMenuBiz roleMenuBiz;

    @Autowired
    UserRoleBiz userRoleBiz;

    public List<Menu> findMenuOfUser(HashMap userInfo) {
        return menuMapper.findMenuOfUser(userInfo);
    }



    public Menu addMenu(Menu menu) {
        Menu parentMenu = null;
        if (StringUtils.isBlank(menu.getParentCode())) {
            menu.setParentCode("0");
        } else {
            Menu param = new Menu();
            param.setUid(menu.getParentCode());
            parentMenu = this.selectOne(param);
        }
        // uid
        menu.setUid(UUID.randomUUID().toString());
        // 设置新菜单的code编码，根据parentCodes查询子菜单的最后一个菜单
        Example example = new Example(Menu.class);
        example.createCriteria().andEqualTo("parentCode", menu.getParentCode());
        example.setOrderByClause("code desc limit 0,1");
        List<Menu> children = this.selectByExample(example);
        String lastCode;
        int num = 1;
        if (children != null && children.size() > 0) {
            lastCode = children.get(0).getCode();
            int len = lastCode.length();
            num = Integer.parseInt(lastCode.substring(len - 3, len));
        }
        String pCode = parentMenu == null ? "" : parentMenu.getCode();
        menu.setCode(pCode + String.format("%03d", num + 1));
        // 排序
        if (menu.getSort() == null) {
            HashMap<String, Object> params = new HashMap<>();
            params.put("parentCode", menu.getParentCode());
            Integer maxSort = menuMapper.selectChildMaxSort(params);
            menu.setSort(Long.valueOf(maxSort == null ? 1 : maxSort + 1));
        }
        menu.setStatus("1");

        String userCode = BaseContextHandler.getCode();
        menu.setCreateUserCode(userCode);
        menu.setCreateTime(new Date());
        this.insert(menu);

        // 新增菜单授权
        String roleCode = null;
        if (parentMenu == null) { // 根节点
            Example e = new Example(UserRole.class);
            e.createCriteria().andEqualTo("userCode", userCode);
            List<UserRole> userRoles = userRoleBiz.selectByExample(e);
            if (userRoles != null && userRoles.size() > 0) {
                roleCode = userRoles.get(0).getRoleCode();
            }
        } else { // 非根节点：取父节点的授权角色
            HashMap<String, Object> userMenu = new HashMap<>();
            userMenu.put("userCode", userCode);
            userMenu.put("menuCode", pCode);
            List<RoleMenu> roleMenus = roleMenuBiz.findRoleMenuByUserMenu(userMenu);
            if (roleMenus != null && roleMenus.size() > 0) {
                roleCode = roleMenus.get(0).getRoleCode();
            }
        }

        if (roleCode != null) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleCode(roleCode);
            roleMenu.setMenuCode(menu.getCode());
            roleMenu.setCreateUserCode(userCode);
            roleMenu.setCreateTime(new Date());
            roleMenu.setStatus("1");
            roleMenuBiz.insert(roleMenu);
        }
        return menu;
    }

    public String deleteAllByCode(String code) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("menuCode", code);
        roleMenuBiz.deleteAllByMenuCode(params);
        params = new HashMap<>();
        params.put("code", code);
        menuMapper.deleteAllByCode(params);
        return code;
    }
}